<?

include_once('top.php');

$project = mysql_real_escape_string($_GET['project']);
$milestone = mysql_real_escape_string($_GET['milestone']);

if (!isset($_GET['name'])) {
	header("location: new_task.php?milestone=$milestone&project=$project");
}

$name = mysql_real_escape_string($_GET['name']);

if (!empty($_POST)) {
	$date = date('Y-m-d', strtotime($_POST['date']));
	$assignee = mysql_real_escape_string($_POST['assignee']);
	$description = mysql_real_escape_string($_POST['description']);
	
	$open = '1';
	
	if(isset($_POST['closed'])) {
		$open = '0';
	}
	
	if (empty($name) || empty($_POST['date']) || empty($assignee)) {
		$error = 'All fields (except description) are required.';
	} else {
		$m = fetch_only("SELECT `Due_date` FROM `MILESTONE` WHERE `Name`='$milestone' AND `Project`='$project'");
		
		if (strtotime($m['Due_date']) >= strtotime($_POST['date'])) {
			run("UPDATE `TASK` SET `Due_date`='$date', `Employee`='$assignee', `Open`='$open', `Description`='$description' WHERE `Name`='$name' AND `Milestone`='$milestone' AND `Project`='$project'");
			$error = 'Task Updated';
		} else {
			$error = 'Invalid due date (the milestone is due on ' . date('m/d/Y', strtotime($m['Due_date'])) . ')';
		}
	}
}

$task = fetch_only("SELECT `Milestone`, `Name`, `Description`, `Due_date`, `Employee`, `Description`, `Open` FROM `TASK` WHERE `Name`='$name' AND `Milestone`='$milestone' AND `Project`='$project'");
format_date($task, 'Due_date', 'm/d/y');

$users = fetch_all("SELECT `First_name`, `Last_name`, `Email` FROM `USER`, `PROJECT_WORKERS` WHERE `PROJECT_WORKERS`.`Employee` = `USER`.`Email` AND `PROJECT_WORKERS`.`Project`='$project'");
$html = '';

for ($i = 0; $i < count($users); $i++) {
	$name = "{$users[$i]['First_name']} {$users[$i]['Last_name']}";
	
	if ($users[$i]['Email'] == $task['Employee']) {
		$html .= '<option value="' . $users[$i]['Email'] . '" selected="true">' . $name . '</option>';
	} else {
		$html .= '<option value="' . $users[$i]['Email'] . '">' . $name . '</option>';
	}
}

?>

<html>
<head>
	<title>GT Project Manager - Task Info</title>
	<link rel="stylesheet" type="text/css" href="main.css" />
	<link rel="stylesheet" type="text/css" href="inner.css" />
</head>
<body>
	<form action="#" method="post">
		<table cellspacing="0" cellpadding="0" id="main">
			<tr><td id="title">
				Task Info
				<div class="nav">
					<a href="login.php?action=logout">Log Out</a><br />
					<?php echo $link ?><br />
					<a href="milestone_info.php?milestone=<?php echo $_GET['milestone'] ?>&project=<?php echo $_GET['project'] ?>">Milestone Info</a><br />
					<a href="time_tracking.php?project=<?php echo $_GET['project'] ?>&milestone=<?php echo $_GET['milestone'] ?>&task=<?php echo $_GET['name'] ?>">Log Hours</a><br />
					<a href="new_task.php?milestone=<?php echo $_GET['milestone'] ?>&project=<?php echo $_GET['project'] ?>">New Task</a>
				</div>
			</td></tr>
			<?php
			if ($error) {
			?>
			<tr><td class="message" colspan="2"><?php echo $error ?></td></tr>
			<?php
			}
			?>
			<tr>
				<td>
					<table class="details" cellspacing="0" cellpadding="0">
						<tr>
							<th>Milestone</th>
							<td><?php echo $milestone ?></td>
						</tr>
						<tr>
							<th>Name</th>
							<td>
								<input type="text" name="name"
									value="<?php echo $task['Name'] ?>" />
							</td>
						</tr>
						<tr>
							<th>Description</th>
							<td>
								<textarea type="text" name="description" resizable="false"><?php echo $task['Description'] ?></textarea>
							</td>
						</tr>
						<tr>
							<th>Due Date</th>
							<td class="date">
								<input type="text" name="date" value="<?php echo $task['Due_date'] ?>" />
							</td>
						</tr>
						<tr>
							<th>Assigned To</th>
							<td>
								<select name="assignee">
									<?php echo $html ?>
								</select>
							</td>
						</tr>
						<tr>
							<th>Closed?</th>
							<?php
							if ($task['Open'] == '1') {
							?>
							<td><input type="checkbox" name="closed" /></td>
							<?php
							} else {
							?>
							<td><input type="checkbox" name="closed" checked="true" /></td>
							<?php
							}
							?>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td class="button_bar">
					<input type="submit" value="Save" />
				</td>
			</tr>
		</table>
	</form>
</body>
</html>
